<template>
  <div class="container">
    <div class="app-container">
      <!-- <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" /> -->
      <el-tree :data="depts" :props="defaultProps" default-expand-all :expand-on-click-node="false">
        <template #default="{data}">
          <el-row style="width: 100%;height: 40px;" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="manager-name">{{ data.managerName }}</span>
              <el-dropdown @command="operateDept($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="enit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <AddDept
      ref="deptOpen"
      :show-dialog.sync="showDialog"
      :current-depept-id="currentDepeptId"
      @updateDepartmentList="getDepartment"
    />
  </div>
</template>
<script>
import { listToTreeList } from '@/utils/index'
import { departmentDataAPI, deleteDepartmentAPI } from '@/api/department'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: { AddDept },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      showDialog: false,
      currentDepeptId: null
    }
  },
  created() {
    // this.$store.dispatch('user/departmentList')
    this.getDepartment()
  },
  methods: {
    handleNodeClick(data) {
      // console.log(data)
    },
    async getDepartment() {
      const list = await departmentDataAPI()
      this.depts = listToTreeList(list, 0)
      // console.log(this.depts)
    },
    operateDept(type, id) {
      if (type === 'add') {
        this.showDialog = true
        this.currentDepeptId = id
      } else if (type === 'enit') {
        this.showDialog = true
        this.currentDepeptId = id
        this.$nextTick(() => {
          this.$refs.deptOpen.getDepartmentDetail()
        })
      } else {
        // $mess
        this.$confirm('确定删除么？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(async() => {
          await deleteDepartmentAPI(id)
          this.$message.success('删除部门成功')
          this.getDepartment()
        })
      }
    }
  }
}
</script>
<style scoped>
.app-container{
  padding: 30px 140px;
  font-size: 14px;
}
.manager-name{
  width: 50px;
  display: inline-block;
  margin-right: 20px;
}
</style>
